package io_demo;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author ChenYongHao
 * @date 2023/4/28 10:25
 * @desc 字节流复制文件的四种方式
 */

public class Byte_IO_Demo {
    public static void main(String[] args) throws IOException {
        method1();
        method2();
        method3();
        method4();
    }

    // 基本字节流一次读写一个字节
    public static void method1() throws IOException {
        FileInputStream fis = new FileInputStream("E:\\cyhKyGit\\Java_Study\\Java_File_IO\\src\\main\\java\\io_demo\\demo.txt");
        FileOutputStream fos = new FileOutputStream("E:\\cyhKyGit\\Java_Study\\Java_File_IO\\src\\main\\java\\io_demo\\democopy.txt");

        int by;
        while ((by = fis.read()) != -1) {
            fos.write(by);
        }

        fos.close();
        fis.close();
    }

    // 基本字节流一次读写一个字节数组
    public static void method2() throws IOException {
        FileInputStream fis = new FileInputStream("E:\\cyhKyGit\\Java_Study\\Java_File_IO\\src\\main\\java\\io_demo\\demo.txt");
        FileOutputStream fos = new FileOutputStream("E:\\cyhKyGit\\Java_Study\\Java_File_IO\\src\\main\\java\\io_demo\\democopy.txt");

        byte[] bys = new byte[1024];
        int len;
        while ((len = fis.read(bys)) != -1) {
            fos.write(bys, 0, len);
        }

        fos.close();
        fis.close();
    }

    // 字节缓冲流一次读写一个字节
    public static void method3() throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\cyhKyGit\\Java_Study\\Java_File_IO\\src\\main\\java\\io_demo\\demo.txt"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\cyhKyGit\\Java_Study\\Java_File_IO\\src\\main\\java\\io_demo\\democopy.txt"));

        int by;
        while ((by = bis.read()) != -1) {
            bos.write(by);
        }

        bos.close();
        bis.close();
    }

    // 字节缓冲流一次读写一个字节数组
    public static void method4() throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\cyhKyGit\\Java_Study\\Java_File_IO\\src\\main\\java\\io_demo\\demo.txt"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\cyhKyGit\\Java_Study\\Java_File_IO\\src\\main\\java\\io_demo\\democopy.txt"));

        byte[] bys = new byte[1024];
        int len;
        while ((len = bis.read(bys)) != -1) {
            bos.write(bys, 0, len);
        }

        bos.close();
        bis.close();
    }
}

